#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include"tcpsocket.h"

void Showusage()
{
	printf("argv[1] server ip\n");
	printf("argv[2] server port\n");
}

int main(int argc,char * argv[])
{
	int ret = -1;
	//test tcp socket as a server socket
	TcpSocket svr(argv[1],atoi(argv[2]));
	ret = svr.Bind();
	if (ret == 0)
	{
		ret = svr.Listen();
		if (ret == 0)
		{
			TcpSocket conn;
			ret = svr.Accept(&conn);
	
			if (ret == 0)
			{
				printf("accept a client.\n");
				char buf[512]={0};
				while ( (ret = conn.Read(buf,sizeof(buf))) !=-1 && ret!=0)
				{
					//write back
					conn.Write(buf,sizeof(buf));
					printf("%s\n",buf);
					fflush(0);
					memset(buf,0,sizeof(buf));
				}
					
			}
		}
	}
	if (ret != 0)
	{
		printf("ret is %d, errno is %d, error msg is %s\n",ret,errno,strerror(errno));
	}
	return  ret;
}
